<template>
  <div>
    <div class="select_top">
      <el-input
        v-model="ryglData.orgname"
        @keyup.native.enter="getRylist"
        placeholder="请输入单位名称"
        style="width:18%;margin-right:10px"
      ></el-input>
      <el-input
        v-model="ryglData.pname"
        @keyup.native.enter="getRylist"
        placeholder="请输入联系人"
        style="width:18%;margin-right:10px"
      ></el-input>
      <el-input
        v-model="ryglData.phone"
        @keyup.native.enter="getRylist"
        placeholder="请输入联系电话"
        style="width:18%;margin-right:10px"
      ></el-input>
      <el-button @click="getRylist">搜索</el-button>
      <el-button type="primary" style="float:right" @click="newRy" v-if="belongunit!='10'">新增</el-button>
    </div>
    <el-table
      ref="filterTable"
      :data="ryglList"
      style="width: 92%;font-size:13px;margin: 20px auto"
    >
      <el-table-column prop="hash.id" label="ID"></el-table-column>
      <el-table-column prop="hash.pname" label="姓名"></el-table-column>
      <el-table-column prop="hash.orgname" label="所属单位"></el-table-column>
      <el-table-column prop="hash.idcard" label="身份证"></el-table-column>
      <el-table-column prop="hash.phone" label="联系电话"></el-table-column>
      <el-table-column label="操作" :width="belongunit!=10?'250':''">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="checkry(scope.row)">查看</el-button>
          <el-button
            type="primary"
            size="small"
            @click="editRy(scope.row)"
            v-if="belongunit!='10'"
          >编辑</el-button>
          <el-button
            type="danger"
            size="small"
            @click="deletery(scope.row)"
            v-if="belongunit!='10'"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :currentPage.sync="currentPage3"
      :pageSize="pagesize"
      layout="prev, pager, next, jumper"
      :total="total"
      style="text-align:center"
    ></el-pagination>

    <el-dialog :title="diatitle" :visible.sync="showRy" width="40%">
      <el-form labelWidth="100px" v-loading="loading">
        <el-form-item label="姓名" class="is-required">
          <el-input clearable v-model="newryData.pname" v-validate="'required'" name="姓名"></el-input>
          <div class="el-form-item__error">{{ errors.first('姓名') }}</div>
        </el-form-item>
        <el-form-item label="所属单位">{{orgname}}</el-form-item>
        <!-- <el-form-item label="所属单位">
          <el-select v-model="newryData.orgname" placeholder="请选择" style="width:100%">
            <el-option
              v-for="item in dwSelect"
              :key="item.hash.orgid"
              :label="item.hash.orgname"
              :value="item.hash.orgid"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="类型" class="is-required">
          <el-select
            v-model="newryData.ptype"
            placeholder="请选择"
            style="width:100%"
            v-validate="'required'"
            name="类型"
          >
            <el-option
              v-for="item in lxSelect"
              :key="item.hash.code_id"
              :label="item.hash.code_name"
              :value="item.hash.code_id"
            ></el-option>
          </el-select>
          <div class="el-form-item__error">{{ errors.first('类型') }}</div>
        </el-form-item>
        <el-form-item label="身份证" class="is-required">
          <el-input clearable v-model="newryData.idcard" v-validate="'required'" name="身份证"></el-input>
          <div class="el-form-item__error">{{ errors.first('身份证') }}</div>
        </el-form-item>
        <el-form-item label="联系电话" class="is-required">
          <el-input clearable v-model="newryData.phone" v-validate="'required'" name="联系电话"></el-input>
          <div class="el-form-item__error">{{ errors.first('联系电话') }}</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="diatitle!=='查看人员'">
        <el-button @click="showRy = false">取 消</el-button>
        <el-button type="primary" @click="saveNewry" v-if="diatitle=='添加人员'">提 交</el-button>
        <el-button type="primary" @click="saveeidtRy" v-else>提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage3: 1,
      pagesize: 10,
      total: 10,
      belongunit: localStorage.getItem("belongunit"),
      loading: true,
      ts: this.newStore(),
      diatitle: "添加人员",
      showRy: false,
      orgname: localStorage.getItem("orgname"),
      ryglData: {
        orgname: "",
        pname: "",
        phone: ""
      },
      newryData: {
        pname: "",
        idcard: "",
        ptype: "",
        phone: "",
        id: ""
      },
      ryglList: [],
      lxSelect: []
      // dwSelect: []
    };
  },
  created() {
    this.getRylist();
  },
  methods: {
        handleCurrentChange() {
      this.getRylist();
    },
    deletery(item) {
      const req = {
        handle:
          "D4C22D9B0E4876FAD400B0C71B331F7DEAA0F765F26818C2F20873636232C279",
        id: item.hash.id
      };
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.spost(this.ts, "/mainservlet", req).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getRylist();
            this.showRy = false;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    saveeidtRy() {
      this.$validator.validateAll().then(result => {
        if (result) {
          const req = {
            handle:
              "D4C22D9B0E4876FAD400B0C71B331F7D6655C600C04B1120F20873636232C279",
            pname: this.newryData.pname,
            idcard: this.newryData.idcard,
            ptype: this.newryData.ptype,
            phone: this.newryData.phone,
            id: this.newryData.id
          };
          this.spost(this.ts, "/mainservlet", req).then(ts => {
            this.$message({
              message: "修改成功！",
              type: "success"
            });
            this.getRylist();
            this.showRy = false;
          });
        } else {
          this.$message.error("内容不能为空！");
          return false;
        }
      });
    },
    checkry(item) {
      this.diatitle = "查看人员";
      this.loading = true;
      this.getSelect();
      this.showRy = true;
      const req = {
        handle:
          "D4C22D9B0E4876FAD400B0C71B331F7D709ECA131DD6D6787F15AF0FE54238A409652941AB86E5AD",
        id: item.hash.id
      };
      this.spost(this.ts, "/mainservlet", req).then(ts => {
        this.newryData = ts.d.data.operation1[0].hash;
        this.loading = false;
      });
    },
    editRy(item) {
      this.loading = true;
      this.diatitle = "编辑人员";
      this.getSelect();
      this.showRy = true;
      const req = {
        handle:
          "D4C22D9B0E4876FAD400B0C71B331F7D709ECA131DD6D6787F15AF0FE54238A409652941AB86E5AD",
        id: item.hash.id
      };
      this.spost(this.ts, "/mainservlet", req).then(ts => {
        this.newryData = ts.d.data.operation1[0].hash;
        this.loading = false;
        console.log(this.newryData);
      });
    },
    newRy(item) {
      this.diatitle = "添加人员";
      this.loading = true;
      this.newryData = {};
      this.getSelect();
      this.showRy = true;
      this.loading = false;
    },
    saveNewry() {
      this.$validator.validateAll().then(result => {
        if (result) {
          const req = {
            handle:
              "D4C22D9B0E4876FAD400B0C71B331F7D7537A89A8570EB44AFC62831F1BC1BA1",
            pname: this.newryData.pname,
            idcard: this.newryData.idcard,
            ptype: this.newryData.ptype,
            phone: this.newryData.phone
          };
          this.spost(this.ts, "/mainservlet", req).then(ts => {
            this.$message({
              message: "添加成功！",
              type: "success"
            });
            this.getRylist();
            this.showRy = false;
          });
        } else {
          this.$message.error("内容不能为空！");
          return false;
        }
      });
    },
    getSelect() {
      // const req = {
      //   handle: "67A5B6FEF17D97DECF116BDF95B95C0BC327730680009A98",
      //   suporgid: "11",
      //   orgname: "新",
      //   managername: "1"
      // };
      const req2 = {
        handle: "67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9",
        supid: "05",
        codetype: "01"
      };
      // this.spost(this.ts, "/mainservlet", req).then(ts => {
      //   this.dwSelect = ts.d.data.operation1;
      // });
      this.spost(this.ts, "/mainservlet", req2).then(ts => {
        this.lxSelect = ts.d.data.operation1;
      });
    },
    getRylist() {
      const req = {
        handle:
          "D4C22D9B0E4876FAD400B0C71B331F7D709ECA131DD6D67818A0B411B7737DDA",
        pname: this.ryglData.pname,
        phone: this.ryglData.phone,
        orgname: this.ryglData.orgname,
        start: (this.currentPage3 - 1) * this.pagesize,
        limit: this.pagesize
      };
      this.spost(this.ts, "/mainservlet", req).then(ts => {
        this.ryglList = ts.d.data.operation1;
        this.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
    }
  }
};
</script>
<style lang="stylus" scoped>
.select_top {
  width: 92%;
  margin: 20px auto;
}
</style>

